package alpha.com.google.code.mechanic.janitor.ui.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import alpha.com.google.code.mechanic.janitor.controller.ContextData;
import alpha.com.google.code.mechanic.janitor.ui.ContextElemMgtDialog;

/**
 * The class is to handle the command of sub menu item: "Context Elements..."
 * of the Workspace Janitor menu.
 * 
 * Last_edited: Oct 14, 2014
 * 
 * @author jingjiejiang
 * @since Aug 4, 2014 
 *
 */
public class CtxConfigsMgtHandler extends AbstractHandler {
  
  private ContextData ctxData = new ContextData();

  public Object execute(ExecutionEvent event) throws ExecutionException {
    
    Shell parentShell = Display.getCurrent().getActiveShell();
    
    try {
      ctxData.readContextsFile();
    } catch (Exception e) {
      MessageDialog.openError(parentShell, "Couldn't read the Contexts data "
          + "from the context.con file", e.getMessage());
    }
    
    ContextElemMgtDialog dialog = new ContextElemMgtDialog(parentShell,
        ctxData.getContextsCollector());
    int dialogopen = dialog.open();
    if (dialogopen == Dialog.OK || dialogopen == Dialog.CANCEL) {
      
      ctxData.getContextsCollector().resetContextList();
      dialog.close(); 
    }
    return null;
  }

}
